package com.t.bricks.bussiness.service.customer.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.customer.CustomerDao;
import com.t.bricks.bussiness.db.entity.customer.Customer;
import com.t.bricks.bussiness.db.entity.customer.CustomerAccount;
import com.t.bricks.bussiness.model.customer.AddEntity;
import com.t.bricks.bussiness.service.customer.CustomerService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.StringUtil;
import com.t.bricks.utils.function.encode.AESUtil;
import com.t.bricks.utils.function.encode.MD5Util;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.function.url.URLUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 顾客信息'Customer表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2022年06月22日 15:25:25
 */
@Service
public class CustomerServiceImpl implements CustomerService<Customer> {

	@Autowired
	private CustomerDao<Customer> customerDao;
	
	/**
	 * md5加密干扰串
	 * 注意,必须与BaseSystem中的干扰串一样,否则无法登录
	 */
	@Value("${app.md5.key:未设置app.md5.key配置项}")
	private String appMd5Key;
	
	/**
	 * 创建sRecordKey用的干扰串
	 */
	@Value("${app.record.key.jam:123456}")
	private String recordKeyJam;
	
	/**
	 * 短信验证码查看地址
	 */
	@Value("${app.identifying.code.find:未设置app.identifying.code.find配置项}")
	private String findCodeUrl;
	
	/**
	 * 短信验证码发送地址
	 */
	@Value("${app.identifying.code.send:未设置app.identifying.code.send配置项}")
	private String sendCodeUrl;
	
	/**
	 * 默认短信验证码
	 */
	@Value("${app.identifying.code.default:4332}")
	private Integer defaultCode;
	
	/**
	 * 新增
	 * @param addEntity 新增顾客对象实体
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity addCustom(AddEntity addEntity) {
		if (null == addEntity) {
			return MsgEmity.err(1001, "数据为空");
		}
		
		if (null == addEntity.getiCode()) {
			return MsgEmity.err(1002, "验证码为空");
		}
		
		if (addEntity.getiCode().intValue() < 1 || addEntity.getiCode().intValue() > 999999) {
			return MsgEmity.err(1003, "验证码最大为6位数");
		}
		
		Customer customer = addEntity.getCustomer();
		CustomerAccount customerAccount = addEntity.getCustomerAccount();
		
		if (StringUtil.isEmpty(customer.getsNo())) {
			return MsgEmity.err(1004, "识别号(电话)不能为空");
		}
		
		MsgEmity me = this.hasByKey("sNo", customer.getsNo());//检查关键值记录是否存在(返回1:存在;0:不存在)
		if (!me.isSuccess() && !me.getData().equals(0)) {//发生异常
			return MsgEmity.err(1005, "验证账户重复失败");
		}
		
		if (me.getData().equals(1)) {
			return MsgEmity.err(1006, "账户已经存在");
		}
		
		//--如果使用了默认验证码(免验证码注册)--//
		if (null == defaultCode || !defaultCode.equals(addEntity.getiCode())) {
			me = URLUtil.get(findCodeUrl.replace("电话", customer.getsNo()));
			if (!me.isSuccess()) {
				return me;
			}
			
			me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
			if (!me.isSuccess()) {
				return me.appendDataCode(7300).insertMsg("验证码错误:");
			}
			
			Map<String, String> identifyingMap = me.getData();
			if (!addEntity.getiCode().equals(identifyingMap.get("iCode"))){
				return MsgEmity.err(1007, "验证码错误");
			}
		}
		
		if (null == customer.getsAliases()) {
			customer.setsAliases(customer.getsName());
		}
		
		customer.setsOwner("BricksMall.Customer");//用户来源绝对不能由外界传入
		customer.setsType("顾客");//用户类型绝对不能由外界传入
		
		me = ModuleUtil.validDbEntityByAdd(customer);// 通用的基本数据验证
		if (!me.isSuccess()) {
			return me;
		}
		
		if (null == customer.getsPass() || "".equals(customer.getsPass().trim())) {
			return MsgEmity.err(1008, "密码不能为空");
		}
		
		if (null == customerAccount.getsPayPass() || "".equals(customerAccount.getsPayPass().trim())) {
			customerAccount.setsPayPass("123456");//设置个默认值,此默认值为明文,与真正密码是不一样的
		} else {
			customerAccount.setsPayPass(MD5Util.md5(customerAccount.getsPayPass(), appMd5Key));//密码必须加密才能入库
		}
		
		if (null != customerAccount.getsPaySignPass() && "".equals(customerAccount.getsPaySignPass().trim())) {
			customerAccount.setsPaySignPass(MD5Util.md5(customerAccount.getsPaySignPass(), appMd5Key));//密码必须加密才能入库
		} else {
			customerAccount.setsPaySignPass("99999");//设置个默认值,此默认值为明文,与真正密码是不一样的
		}
		
		customerAccount.setmFund(BigDecimal.ZERO);//新建账号必须清除余额
		customerAccount.setiTally(0);//新建账号必须清除积分
		
		me = ModuleUtil.validDbEntityByAdd(customerAccount);
		if (!me.isSuccess()) {
			return me;
		}
		
		me = this.validByAdd(customer);
		if (!me.isSuccess()) {
			return me;
		}
		
		customer.setuId(null);//清空记录编号
		customer.setsAesPass(AESUtil.encode(customer.getsPass(), appMd5Key));//密码必须加密才能入库
		customer.setsPass(MD5Util.md5(customer.getsPass(), appMd5Key));//密码必须加密才能入库
		
		if (null != customer.getsSignPass() && !"".equals(customer.getsSignPass().trim())) {
			customer.setsSignPass(MD5Util.md5(customer.getsSignPass(), appMd5Key));//密码必须加密才能入库
		} else {
			customer.setsSignPass("99999");//设置个默认值,此默认值为明文,与真正密码是不一样的
		}
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(customer);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}
		
		customerAccount.setsId(customer.getuId());//设定记录编号
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(customerAccount, customer.getuId(), null);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}
		
		customerAccount.setsCreator(customer.getuId());//特殊,必须在次赋予
		customerAccount.setsModifieder(customer.getuId());//特殊,必须在次赋予
		customerAccount.setsRecordKey(customerAccount.createRecordKey(recordKeyJam));//特殊,必须在次赋予
		
		return customerDao.addCustom(customer, customerAccount);
	}
	
	/**
	 * 修改密码
	 * @param uId
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity editPass(String uId, String sOldPass, String sNewPass) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "用户uId不能为空！", 8001);
		}
		
		if (null == sOldPass || "".equals(sOldPass.trim())) {
			return new MsgEmity(false, "旧密码不能为空！", 8002);
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return new MsgEmity(false, "新密码不能为空！", 8003);
		}

		MsgEmity me = customerDao.hasById(uId);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "没有查找到用户！", 8004);
		}
		
		me = customerDao.findById(uId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到对应用户！", 8005);
		}
		
		Customer customer = me.getData();
		
		if (customer.getiState() != 1) {
			return new MsgEmity(false, "用户被限制！", 8006);
		}
		
		String sOldPassMd5 = MD5Util.md5(sOldPass, appMd5Key);
		if (!sOldPassMd5.equals(customer.getsPass()) ) {
			return new MsgEmity(false, "提供的旧密码错误！", 8007);
		}
		
		String sNewPassMd5 = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPassMd5.equals(sOldPassMd5)) {
			return MsgEmity.success(8999, "密码无需修改！");
		}
		
		String sAesPass = AESUtil.encode(sNewPass, appMd5Key);
		
		return customerDao.editPass(uId,
				sOldPassMd5,
				sNewPassMd5,
				sAesPass,
				customer.getiVersion(),
				ModuleUtil.currentLoginUserId());
	}
	
	/**
	 * 重置密码
	 * @param uId 被修改的用户编号
	 * @param sPass 操作人员的密码
	 * @return
	 */
	@Override
	public MsgEmity resetPass(String uId, String sPass) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "用户编号不能为空！", 8001);
		}
		
		uId = uId.trim();
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8002);
		}

		if (!"/00000000/00000001/".contains(currentCustomerId) && !uId.equals(currentCustomerId)) {//超级管理员登录/管理员或当前用户
			return new MsgEmity(false, "仅允许管理员或当前用户重置密码", 8003);
		}
		
		MsgEmity me = customerDao.findById(currentCustomerId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "当前操作用户信息错误,请联系管理员！", 8004);
		}
		
		Customer customerModifieder = me.getData();

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!(sModifiederPass.equals(customerModifieder.getsPass()))) {
			return new MsgEmity(false, "提供的操作用户密码错误!", 8005);
		}
		
		me = customerDao.findById(uId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到待重置密码的用户！", 8006);
		}
		
		Customer customer = me.getData();

		String sOldPass = customer.getsPass();
		String sNewPass = MD5Util.md5("123456", appMd5Key);
		if (sOldPass.equals(sNewPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}

		String sAesPass = AESUtil.encode("123456", appMd5Key);
		
		return customerDao.editPass(uId,
				sOldPass,
				sNewPass,
				sAesPass,
				customer.getiVersion(),
				currentCustomerId);
	}
	
	/**
	 * 修改手势密码
	 * @param uId
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity editSignPass(String uId, String sOldPass, String sNewPass) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "用户uId不能为空！", 8001);
		}
		
		if (null == sOldPass || "".equals(sOldPass.trim())) {
			return new MsgEmity(false, "旧密码不能为空！", 8002);
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return new MsgEmity(false, "新密码不能为空！", 8003);
		}

		Customer customerData = new Customer();
		customerData.setuId(uId);
		MsgEmity me = customerDao.findCountByKey(customerData, false);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "没有查找到用户！", 8004);
		}
		
		me = customerDao.findById(uId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到对应用户！", 8005);
		}
		
		Customer customer = me.getData();
		
		if (customer.getiState() != 1) {
			return new MsgEmity(false, "用户被限制！", 8006);
		}
		
		String sOldPassMd5 = MD5Util.md5(sOldPass, appMd5Key);
		if (!sOldPassMd5.equals(customer.getsSignPass()) ) {
			return new MsgEmity(false, "提供的旧密码错误！", 8007);
		}
		
		String sNewPassMd5 = MD5Util.md5(sNewPass, appMd5Key);
		if (sNewPassMd5.equals(sOldPassMd5)) {
			return MsgEmity.success(8999, "密码无需修改！");
		}
		
		return customerDao.editSignPass(uId,
				sOldPassMd5,
				sNewPassMd5,
				customer.getiVersion(),
				ModuleUtil.currentLoginUserId());
	}
	
	/**
	 * 重置手势密码
	 * @param uId 被修改的用户编号
	 * @param sPass 操作人员的密码
	 * @return
	 */
	@Override
	public MsgEmity resetSignPass(String uId, String sPass) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "用户编号不能为空！", 8001);
		}
		
		uId = uId.trim();
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8002);
		}

		if (!"/00000000/00000001/".contains(currentCustomerId) && !uId.equals(currentCustomerId)) {//超级管理员登录/管理员或当前用户
			return new MsgEmity(false, "仅允许管理员或当前用户重置密码", 8003);
		}
		
		MsgEmity me = customerDao.findById(currentCustomerId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "当前操作用户信息错误,请联系管理员！", 8004);
		}
		
		Customer customerModifieder = me.getData();

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!(sModifiederPass.equals(customerModifieder.getsPass()))) {
			return new MsgEmity(false, "提供的操作用户密码错误!", 8005);
		}
		
		me = customerDao.findById(uId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到待重置密码的用户！", 8006);
		}
		
		Customer customer = me.getData();

		String sOldPass = customer.getsSignPass();
		String sNewPass = MD5Util.md5("123456", appMd5Key);
		if (sOldPass.equals(sNewPass)) {
			return MsgEmity.success(8999, "密码无需修改重置！");
		}
		
		me = customerDao.editSignPass(uId,
				sOldPass,
				sNewPass,//按123456进行设置密码
				customer.getiVersion(),
				currentCustomerId);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.appendMsg("当前手势密码为123456");
	}

	/**
	 * 验证当前账号密码
	 * @param sId 账号
	 * @param sPass 待验证密码
	 * @return
	 */
	@Override
	public MsgEmity checkPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8101, "账号参数为空");
		}

		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8102, "待验证密码参数为空");
		}
		
		MsgEmity me = this.getValueByFieldName(sId, Arrays.asList("sPass", "sCreator"), true);
		if (!me.isSuccess()) {
			return me;
		}
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8103);
		}
		
		Customer customer = me.getData();
		
		if (!currentCustomerId.equals(customer.getsCreator())) {
			return new MsgEmity(false, "账户与创建者信息不一致", 8104);
		}

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!sModifiederPass.equals(customer.getsPass())) {
			return new MsgEmity(false, "密码错误!", 8105);
		}
		
		return MsgEmity.success("密码正确");
	}

	/**
	 * 验证当前账号手势密码
	 * @param sId 账号
	 * @param sPass 待验证手势密码
	 * @return
	 */
	@Override
	public MsgEmity checkSignPass(String sId, String sPass) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(8101, "账号参数为空");
		}

		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8102, "待验证手势密码参数为空");
		}
		
		MsgEmity me = this.getValueByFieldName(sId, Arrays.asList("sSignPass", "sCreator"), true);
		if (!me.isSuccess()) {
			return me;
		}
		
		String currentCustomerId = ModuleUtil.currentLoginUserId();
		if (null == currentCustomerId || "".equals(currentCustomerId.trim())) {
			return new MsgEmity(false, "未能获取操作人员信息！", 8103);
		}
		
		Customer customer = me.getData();
		
		if (!currentCustomerId.equals(customer.getsCreator())) {
			return new MsgEmity(false, "账户与创建者信息不一致", 8104);
		}

		String sModifiederPass = MD5Util.md5(sPass, appMd5Key);
		if (!sModifiederPass.equals(customer.getsSignPass())) {
			return new MsgEmity(false, "密码错误!", 8103);
		}
		
		return MsgEmity.success("密码正确");
	}

	/**
	 * 请求发送手机验证码
	 * @param sPhone 手机
	 * @return
	 */
	@Override
	public MsgEmity sendIdentifyingCode(String sPhone) {
		if (null == sPhone || "".equals(sPhone.trim())) {
			return MsgEmity.err(8001, "手机参数为空");
		}

		MsgEmity me = URLUtil.get(sendCodeUrl.replace("电话", sPhone.trim()));
		if (!me.isSuccess()) {
			return me;
		}
		
		me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
		if (!me.isSuccess()) {
			return me.appendDataCode(7000).insertMsg("请求验证码错误:");
		}
		
		return MsgEmity.success("请求发送验证码成功,手机号:", sPhone);
	}
	
	/**
	 * 忘记密码
	 * @param sNo 识别号(电话)
	 * @param iCode 短信验证码
	 * @param sNewPass 新密码
	 * @return
	 */
	@Override
	public MsgEmity forgetPass(String sNo, Integer iCode, String sNewPass) {
		if (null == sNo || "".equals(sNo.trim())) {
			return MsgEmity.err(8001, "识别号(电话)参数为空");
		}

		if (null == iCode || iCode.intValue() < 1) {
			return MsgEmity.err(8002, "短信验证码参数为空");
		}
		
		if (null == sNewPass || "".equals(sNewPass.trim())) {
			return MsgEmity.err(8003, "新密码参数为空");
		}
		
		sNo = sNo.trim();
		sNewPass = sNewPass.trim();
		
		MsgEmity me = URLUtil.get(findCodeUrl.replace("电话", sNo.trim()));
		if (!me.isSuccess()) {
			return me;
		}
		
		me = JacksonShell.toJavaObject(me.getData().toString(), MsgEmity.class);
		if (!me.isSuccess()) {
			return me.appendDataCode(7300).insertMsg("验证码错误:");
		}
		
		Map<String, String> identifyingMap = me.getData();
		if (!iCode.equals(identifyingMap.get("iCode"))){
			return MsgEmity.err("验证码错误");
		}
		
		Customer customer = new Customer();
		customer.setsNo(sNo);
		me = customerDao.findByKey(customer, false);
		if (!me.isSuccess()) {
			return me.appendDataCode(10);
		}
		
		List<Customer> list = me.getData();
		if (list.size() < 1) {
			return MsgEmity.err(8004, "没有找到对应的顾客数据");
		}
		
		customer = list.get(0);

		String sOldPass = customer.getsPass();
		String sNewPassMd5 = MD5Util.md5(sNewPass, appMd5Key);
		if (sOldPass.equals(sNewPassMd5)) {
			return MsgEmity.success(8999, "密码无需修改！");
		}

		String sAesPass = AESUtil.encode(sNewPass, appMd5Key);
		
		return customerDao.editPass(
				customer.getuId(),
				sOldPass,
				sNewPassMd5,
				sAesPass,
				customer.getiVersion(),
				customer.getsCreator());
	}

	/**
	 * 删除数据
	 * 注意:按公司要求删除账户,不进行判断账户是否存在金额
	 * @param id 记录编号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(String id) {
		if (null == id || "".equals(id.trim())) {
			return MsgEmity.err(8001, "记录编号参数为空");
		}
		
		if ("00000000/00000001".contains(id)) {
			return MsgEmity.err(8002, "敏感编号,禁止删除");
		}
		
		return customerDao.del(id.trim());
	}
	
	/**
	 * 根据手机号(sNo)与关注号进行登录
	 * @param sNo 识别号(电话)
	 * @param sOpenId 关注号
	 * @param iType 类型(1:微信)
	 * @param sOwner 用户来源表
	 * @param iDevice 设备类型,1:PC,2:手机,3:平板,4.....
	 * @return
	 */
	@Override
	public MsgEmity login(String sNo, String sOpenId, Integer iType, String sOwner, Integer iDevice) {
		if (null == sNo || "".equals(sNo.trim())) {
			return MsgEmity.err(8001, "识别号(电话)参数为空");
		}

		if (null == sOpenId || "".equals(sOpenId.trim())) {
			return MsgEmity.err(8002, "关注号参数为空");
		}
		
		MsgEmity me = customerDao.findAesPass(sNo, sOpenId);
		if (!me.isSuccess()) {
			return me;
		}
		
		String sPass = AESUtil.decode(me.getData().toString(), appMd5Key);
		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8003, "ASE密码解密失败");
		}
		
		me = ModuleUtil.callServiceMethod("loginServiceImpl", "in",
				new Class<?>[] {String.class, String.class, String.class, Integer.class},
				new Object[]{sNo, sPass, sOwner, iDevice});
		if (!me.isSuccess()) {
			return me.appendDataCode(100);
		}
		
		return me;
	}
	
	/**
	 * 根据手机号(sNo)判断是否与关注号进行绑定
	 * @param sNo 识别号(电话)
	 * @param iType 类型(1:微信)
	 * @return
	 */
	@Override
	public MsgEmity hasOpenId(String sNo, Integer iType) {
		MsgEmity me = customerDao.hasOpenId(sNo);
		if (!me.isSuccess()) {
			return me;
		}
		
		if ("".equals(me.getData()) || "-1".equals(me.getData())) {
			return MsgEmity.err(8001, "未绑定");
		}
		
		return MsgEmity.success(8999, "已经绑定");// me.setMsg("已经绑定");
	}
	
	/**
	 * 绑定关注号
	 * @param uId 记录编号
	 * @param sOpenId 关注号
	 * @param sPass 当前密码
	 * @param iType 类型(1:微信)
	 * @return
	 */
	@Override
	public MsgEmity bindingOpenId(String uId, String sOpenId, String sPass, Integer iType) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "用户编号不能为空！", 8001);
		}
		
		if (null == sOpenId || "".equals(sOpenId.trim())) {
			return new MsgEmity(false, "关注号不能为空！", 8002);
		}
		
		if (null == sPass || "".equals(sPass.trim())) {
			return new MsgEmity(false, "密码不能为空！", 8003);
		}

		MsgEmity me = customerDao.hasById(uId);
		if (!me.isSuccess()) {
			return new MsgEmity(false, "没有查找到用户！", 8004);
		}
		
		me = customerDao.findById(uId, false);
		if (!me.isSuccess() || null == me.getData()) {
			return new MsgEmity(false, "没有查找到对应用户！", 8005);
		}
		
		Customer customer = me.getData();
		
		if (customer.getiState() != 1) {
			return new MsgEmity(false, "用户被限制！", 8006);
		}
		
		String sPassMd5 = MD5Util.md5(sPass, appMd5Key);
		if (!sPassMd5.equals(customer.getsPass()) ) {
			return new MsgEmity(false, "提供的密码错误！", 8007);
		}
		
		String sAesPass = AESUtil.encode(sPass, appMd5Key);
		
		return customerDao.bindingOpenId(uId, sOpenId, iType, sAesPass);
	}
	
	/**
	 * 设置微信用户统一标识
	 * @param sWechatUid 微信用户统一标识
	 * @return
	 */
	@Override
	public MsgEmity setWechatid(String sWechatUid) {
		if (null == sWechatUid || "".equals(sWechatUid.trim())) {
			return new MsgEmity(false, "微信用户统一标识不能为空！", 8001);
		}
		
		MsgEmity me = customerDao.hasWechatUid(sWechatUid);
		
		if (me.isSuccess()) {
			return MsgEmity.err(8002, "此微信统一标识已经绑定手机用户:", me.getData().toString());
		}
		
		if (((Integer)me.getData()).intValue() != 7001) {
			return me;//异常
		}
		
		return customerDao.setWechatid(ModuleUtil.currentLoginUserId(), sWechatUid);
	}
	
	/**
	 * 根据微信用户统一标识获取简洁信息
	 * @param sWechatUid 微信用户统一标识
	 * @return
	 */
	@Override
	public MsgEmity getNoByWechatUid(String sWechatUid) {
		if (null == sWechatUid || "".equals(sWechatUid.trim())) {
			return new MsgEmity(false, "微信用户统一标识不能为空！", 8001);
		}
		
		return customerDao.getNoByWechatUid(sWechatUid);
	}
	
	/**
	 * 判断微信用户唯一标识是否已经绑定
	 * @param sWechatUid 微信用户统一标识
	 * @return 返回手机号
	 */
	@Override
	public MsgEmity hasWechatUid(String sWechatUid) {
		MsgEmity me = customerDao.hasWechatUid(sWechatUid);
		if (!me.isSuccess()) {
			return me;
		}
		
		if ("".equals(me.getData()) || "-1".equals(me.getData())) {
			return MsgEmity.err(8001, "未绑定");
		}
		
		return me.setMsg("已经绑定");
	}

	/**
	 * 根据微信用户唯一标识进行登录
	 * @param sWechatUid 微信用户统一标识
	 * @param iDevice 设备类型,1:PC,2:手机,3:平板,4.....
	 * @return
	 */
	@Override
	public MsgEmity loginByWechatUid(String sWechatUid, Integer iDevice) {
		if (null == sWechatUid || "".equals(sWechatUid.trim())) {
			return MsgEmity.err(8001, "微信用户唯一标识参数为空");
		}

		MsgEmity me = customerDao.findAesPassByWechatUid(sWechatUid);
		if (!me.isSuccess()) {
			return me;
		}
		
		Map<String, Object> map = me.getData();
		
		String sAesPass = map.get("sAesPass").toString();
		
		String sPass = AESUtil.decode(sAesPass, appMd5Key);
		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8002, "ASE密码解密失败");
		}
		
		iDevice = iDevice == null ? 2 : iDevice;
		
		me = ModuleUtil.callServiceMethod("loginServiceImpl", "in",
				new Class<?>[] {String.class, String.class, String.class, Integer.class},
				new Object[]{map.get("sNo"), sPass, map.get("sOwner"), iDevice});
		if (!me.isSuccess()) {
			return me.appendDataCode(100);
		}
		
		return me;
	}

}
